<template>
  <!-- wode我的咸鱼 -->
  <div>
    <div class="returnbox">
      <div>
        <router-link to="/home">
          <div class="returnicon">
            <van-icon name="arrow-left" />
          </div>
          <div class="returntext">
            <span>返回</span>
          </div>
        </router-link>
      </div>
      <div class="titleText">
        <span>我的咸鱼</span>
      </div>
    </div>
    <!-- 商品 -->
    <!-- 瀑布流 -->
    <div class="waterfalls-flow">
      <ul ref="left" class="waterfalls-flow-left">
        <!-- 加载左侧列表 -->
        <li v-for="(item,index) in leftList" :key="index">
          <img :src="item.coverImgUrl" alt />
          <div>
            {{item.name}}
            <br />
            ￥{{item.price}}.00
          </div>
        </li>
      </ul>
      <ul ref="right" class="waterfalls-flow-right">
        <!-- 加载you右侧列表 -->
        <li v-for="(item,index) in rightList" :key="index">
          <img :src="item.coverImgUrl" alt />
          <div>
            {{item.name}}
            <br />
            ￥{{item.price}}.00
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { myFishList } from "../../api/shop-api/shop-api";
export default {
  data() {
    return {
      list: [],
      leftList: [],
      rightList: []
    };
  },
  created() {
    //数据初始化
    myFishList(1, 20).then(res => {
      // console.log(res);
      if (res.code === 0) {
        //请求成功
        this.list = res.rows;
        this.setList();
      } else {
        //请求失败，提示（可以在拦截器做统一的失败提示
      }
    });
  },
  methods: {
    //递归调用
    setList() {
      if (this.list.length === 0) {
        return;
      }
      let isleft;
      if (this.$refs.left.offsetHeight <= this.$refs.right.offsetHeight) {
        isleft = true;
        this.leftList.push(this.list.shift());
      } else {
        isleft = false;
        this.rightList.push(this.list.shift());
      }
      //递归调用
      // this.setList();
      this.$nextTick(() => {
        let imgDomObj = isleft
          ? this.$refs.left.children[this.leftList.length - 1].firstElementChild
          : this.$refs.right.children[this.rightList.length - 1]
              .firstElementChild;
        // debugger
        imgDomObj.onload = imgDomObj.onerror = () => {
          this.setList();
        };
      });
    }
  }
};
</script>
<style lang="less" scoped>
.returnbox {
  width: 100%;
  height: 30px;
  padding-top: 10px;
  padding-left: 3px;
  background-color: rgb(252, 93, 31);
}
.returnicon {
  float: left;
  margin-top: 3px;
  color: #ffffff;
}
.returntext {
  float: left;
  color: #ffffff;
}
.titleText {
  margin-left: 40%;
  color: #ffffff;
}

// pubuliu
.waterfalls-flow {
  > ul {
    float: left;
    width: 50%;
    padding: 5px;
    box-sizing: border-box;
    // border: 10px solid purple;
    img {
      width: 100%;
      height: auto;
    }
    li {
      padding: 10px 0;
      text-align: center;
    }
  }
}
</style>